<?php
abstract class AbstractAccount
{
	const ACCOUNT_CREATED = 0;
	const ACCOUNT_LOADED = 1;
	const ACCOUNT_CHANGED = 2;
	
	// account data
	protected $data;
	protected $condition;
	public $passwordencrypted;
	
	public function __construct($data)
	{
		// null or empty array
		if(empty($data))
		{
			$this->data = new ArrayObject(array());
			$this->condition = AbstractAccount::ACCOUNT_CREATED;
			$this->passwordencrypted = false;
		}
		// array
		elseif(is_array($data))
		{
			$this->data = new ArrayObject($data);
			$this->condition = AbstractAccount::ACCOUNT_LOADED;
			$this->passwordencrypted = true;
		}
		elseif(is_object($data) && get_class($data) === 'ArrayObject')
		{
			$this->data = $data;
			$this->condition = AbstractAccount::ACCOUNT_LOADED;
			$this->passwordencrypted = true;
		}
		else
			throw new Exception('Unable too create Account object.');
	}
	
	// // GET
	protected function _get($key)
	{
		if(!$this->data->offsetExists($key))
			return null;
			
		return $this->data->offsetGet($key);
	}
	
	protected function _getCount()
	{
		$stmt = $this->connection->query('SELECT COUNT(*) FROM accounts');
		$row = $stmt->fetch(DataBase::FETCH_NUM);
		$stmt->closeCursor();
		unset($stmt);
	
		return $row[0];
	}
	
	public function getId()
	{
		return $this->_get('id');
	}
	
	public function getName()
	{
		return $this->_get('name');
	}
	
	public function getPassword()
	{
		return $this->_get('password');
	}
	
	public function getEmail()
	{
		return $this->_get('email');
	}
	
	public function getPremiumEnd()
	{
		return $this->_get('premend');
	}
	
	public function getBlocked()
	{
		return $this->_get('blocked');
	}
	
	
	// // SET
	protected function _set($key, $value)
	{
		$this->data->offsetSet($key, $value);
		if($this->condition === AbstractAccount::ACCOUNT_LOADED)
			$this->condition = AbstractAccount::ACCOUNT_CHANGED;
			
		return true;
	}
	
	public function setId($Id)
	{
		// unisinged
		if(!is_int($Id) || $Id < 0)
			return false;
		
		// don't change id if it was set
		if(!$this->data->offsetExists('id'))
			return false;
		
		return $this->_set('id', $Id);
	}
	
	public function setName($name)
	{
		// validation on SAVE in menager
		return $this->_set('name', $name);
	}
	
	public function setPassword($password)
	{
		// validation and encrypt on SAVE in menager
		return $this->_set('password', $password);
	}
	
	public function setEmail($email)
	{
		// validation on SAVE in menager
		return $this->_set('email', $email);
	}
	
	public function setPremend($linuxTime)
	{
		// unisinged
		if(!is_int($Id) || $Id < 0)
			return false;
		
		return $this->_set('premend', $linuxTime);
	}
	
	public function setBlocked($value)
	{
		// 0 or 1
		if(!is_int($Id) || ($Id != 0 && $Id != 1))
			return false;
			
		return $this->_set('blocked', $value);
	}
	// // GET CONDITION
	public function getAccountCondition()
	{
		return $this->condition;
	}
	
	public function setAccountCondition($value)
	{
		if($value !== AbstractAccount::ACCOUNT_LOADED && $value !== AbstractAccount::ACCOUNT_CHANGED && $value !== AbstractAccount::ACCOUNT_CREATED)
			return false;
			
		$this->condition = $value;
		return true;
	}
	
	public function getBaseArrayCopy()
	{
		return $this->data->getArrayCopy();
	}
}

class Account extends AbstractAccount
{
	public function __construct($data)
	{
		parent::__construct($data);
	}
	
	
}

?>